package borderPatrol.misc;

public class Judge {
	
	private static Judge instance;
	private int[] poacherGoal = new int[] {3, 3};
	
	private Judge() {
	}

	public static Judge getInstance() {
		
		if(instance == null) {
			instance = new Judge();
		}
		
		return instance;
	}
	
	public int isTerminal(String id1, String id2) {
		
		if(id1.length() < 7) {
			return 0;
		}
		
		String poacherId;
		String patrollerId;
		
		if(id1.startsWith("1")) {
			poacherId = id1;
			patrollerId = id2;
		} else {
			poacherId = id2;
			patrollerId = id1;
		}
		
		int[] poacherPosition = new int[]{Integer.parseInt(poacherId.substring(poacherId.length() - 2, poacherId.length() - 1)),
										  Integer.parseInt(poacherId.substring(poacherId.length() - 1, poacherId.length()))};
		int[] patrollerPosition = new int[]{Integer.parseInt(patrollerId.substring(patrollerId.length() - 2, patrollerId.length() - 1)),
				  							Integer.parseInt(patrollerId.substring(patrollerId.length() - 1, patrollerId.length()))};
		int[] lastPoacherPosition = new int[]{Integer.parseInt(poacherId.substring(poacherId.length() - 5, poacherId.length() - 4)),
				  						      Integer.parseInt(poacherId.substring(poacherId.length() - 4, poacherId.length() - 3))};
		int[] lastPatrollerPosition = new int[]{Integer.parseInt(patrollerId.substring(patrollerId.length() - 5, patrollerId.length() - 4)),
											    Integer.parseInt(patrollerId.substring(patrollerId.length() - 4, patrollerId.length() - 3))};
		
		return isTerminal(poacherPosition, patrollerPosition, lastPoacherPosition, lastPatrollerPosition);
	}
	
	public int isTerminal(int[] poacherPosition, int[] patrollerPosition, int[] lastPoacherPosition, int[] lastPatrollerPosition) {

		if (lastPatrollerPosition == null || lastPoacherPosition == null) {
			return 0;
		}

		if (isPoacherOnFinalPosition(poacherPosition)) {
			return 1;
		}

		if (areOnSamePosition(poacherPosition, patrollerPosition) || wentThroughSameEdge(lastPoacherPosition, lastPatrollerPosition, poacherPosition, patrollerPosition)) {
			return 2;
		}

		return 0;
	}
	
	public boolean isPoacherOnFinalPosition(int[] poacherPosition) {
		return poacherPosition[0] == poacherGoal[0] && poacherPosition[1] == poacherGoal[1];
	}

	protected boolean wentThroughSameEdge(int[] lastPoacherPosition, int[] lastPatrollerPosition, int[] poacherPosition, int[] patrollerPosition) {
		return isPatrollerOnPoachersLastPosition(lastPoacherPosition, patrollerPosition) && isPoacherOnPatrollersLastPosition(lastPatrollerPosition, poacherPosition);
	}

	protected boolean isPatrollerOnPoachersLastPosition(int[] lastPoacherPosition, int[] patrollerPosition) {
		return lastPoacherPosition[0] == patrollerPosition[0] && lastPoacherPosition[1] == patrollerPosition[1];
	}

	protected boolean isPoacherOnPatrollersLastPosition(int[] lastPatrollerPosition, int[] poacherPosition) {
		return lastPatrollerPosition[0] == poacherPosition[0] && lastPatrollerPosition[1] == poacherPosition[1];
	}

	protected boolean areOnSamePosition(int[] poacherPosition, int[] patrollerPosition) {
		return poacherPosition[0] == patrollerPosition[0] && poacherPosition[1] == patrollerPosition[1] && poacherPosition[0] != -1;
	}
}
